<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head>
<title>Unlock Notification</title>
<style type="text/css">
body {
    margin: auto;
    font-family: Verdana, sans-serif;
    padding: 8px 1%;
}

a { color: #044a64 }
a:visited { color: #734559 }

.logo { position:absolute; margin:3px; }
.tagline {
  float:right;
  text-align:right;
  font-style:italic;
  width:300px;
  margin:12px;
  margin-top:58px;
}

.toolbar {
  text-align: center;
  line-height: 1.6em;
  margin: 0;
  padding: 0px 8px;
}
.toolbar a { color: white; text-decoration: none; padding: 6px 12px; }
.toolbar a:visited { color: white; }
.toolbar a:hover { color: #044a64; background: white; }

.content    { margin: 5%; }
.content dt { font-weight:bold; }
.content dd { margin-bottom: 25px; margin-left:20%; }
.content ul { padding:0px; padding-left: 15px; margin:0px; }

/* rounded corners */
.se  { background: url(../images/se.gif) 100% 100% no-repeat #044a64}
.sw  { background: url(../images/sw.gif) 0% 100% no-repeat }
.ne  { background: url(../images/ne.gif) 100% 0% no-repeat }
.nw  { background: url(../images/nw.gif) 0% 0% no-repeat }

/* Things for "fancyformat" documents start here. */
.fancy img+p {font-style:italic}
.fancy .codeblock i { color: darkblue; }
.fancy h1,.fancy h2,.fancy h3,.fancy h4 {font-weight:normal;color:#044a64}
.fancy h2 { margin-left: 10px }
.fancy h3 { margin-left: 20px }
.fancy h4 { margin-left: 30px }
.fancy th {white-space:nowrap;text-align:left;border-bottom:solid 1px #444}
.fancy th, .fancy td {padding: 0.2em 1ex; vertical-align:top}
.fancy #toc a        { color: darkblue ; text-decoration: none }
.fancy .todo         { color: #AA3333 ; font-style : italic }
.fancy .todo:before  { content: 'TODO:' }
.fancy p.todo        { border: solid #AA3333 1px; padding: 1ex }
.fancy img { display:block; }
.fancy :link:hover, .fancy :visited:hover { background: wheat }
.fancy p,.fancy ul,.fancy ol { margin: 1em 5ex }
.fancy li p { margin: 1em 0 }
/* End of "fancyformat" specific rules. */

</style>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
  
</head>
<body>
<div><!-- container div to satisfy validator -->

<a href="../index.html">
<img class="logo" src="../images/sqlite370_banner.gif" alt="SQLite Logo"
 border="0"></a>
<div><!-- IE hack to prevent disappearing logo--></div>
<div class="tagline">Small. Fast. Reliable.<br>Choose any three.</div>

<table width=100% style="clear:both"><tr><td>
  <div class="se"><div class="sw"><div class="ne"><div class="nw">
  <table width=100% style="padding:0;margin:0;cell-spacing:0"><tr>
  <td width=100%>
  <div class="toolbar">
    <a href="../about.html">About</a>
    <a href="../sitemap.html">Sitemap</a>
    <a href="../docs.html">Documentation</a>
    <a href="../download.html">Download</a>
    <a href="../copyright.html">License</a>
    <a href="../news.html">News</a>
    <a href="../support.html">Support</a>
  </div>
<script>
  gMsg = "Search SQLite Docs..."
  function entersearch() {
    var q = document.getElementById("q");
    if( q.value == gMsg ) { q.value = "" }
    q.style.color = "black"
    q.style.fontStyle = "normal"
  }
  function leavesearch() {
    var q = document.getElementById("q");
    if( q.value == "" ) { 
      q.value = gMsg
      q.style.color = "#044a64"
      q.style.fontStyle = "italic"
    }
  }
</script>
<td>
    <div style="padding:0 1em 0px 0;white-space:nowrap">
    <form name=f method="GET" action="http://www.sqlite.org/search">
      <input id=q name=q type=text
       onfocus="entersearch()" onblur="leavesearch()" style="width:24ex;padding:1px 1ex; border:solid white 1px; font-size:0.9em ; font-style:italic;color:#044a64;" value="Search SQLite Docs...">
      <input type=submit value="Go" style="border:solid white 1px;background-color:#044a64;color:white;font-size:0.9em;padding:0 1ex">
    </form>
    </div>
  </table>
</div></div></div></div>
</td></tr></table>
<div class=startsearch></div>
  
<a href="intro.html"><h2>SQLite C Interface</h2></a><h2>Unlock Notification</h2><blockquote><pre>int sqlite3_unlock_notify(
  sqlite3 *pBlocked,                          /* Waiting connection */
  void (*xNotify)(void **apArg, int nArg),    /* Callback function to invoke */
  void *pNotifyArg                            /* Argument to pass to xNotify */
);
</pre></blockquote><p>
When running in shared-cache mode, a database operation may fail with
an <a href="../c3ref/c_abort.html">SQLITE_LOCKED</a> error if the required locks on the shared-cache or
individual tables within the shared-cache cannot be obtained. See
<a href="../sharedcache.html">SQLite Shared-Cache Mode</a> for a description of shared-cache locking.
This API may be used to register a callback that SQLite will invoke
when the connection currently holding the required lock relinquishes it.
This API is only available if the library was compiled with the
<a href="../compile.html#enable_unlock_notify">SQLITE_ENABLE_UNLOCK_NOTIFY</a> C-preprocessor symbol defined.</p>

<p>See Also: <a href="../unlock_notify.html">Using the SQLite Unlock Notification Feature</a>.</p>

<p>Shared-cache locks are released when a database connection concludes
its current transaction, either by committing it or rolling it back.</p>

<p>When a connection (known as the blocked connection) fails to obtain a
shared-cache lock and SQLITE_LOCKED is returned to the caller, the
identity of the database connection (the blocking connection) that
has locked the required resource is stored internally. After an
application receives an SQLITE_LOCKED error, it may call the
sqlite3_unlock_notify() method with the blocked connection handle as
the first argument to register for a callback that will be invoked
when the blocking connections current transaction is concluded. The
callback is invoked from within the <a href="../c3ref/step.html">sqlite3_step</a> or <a href="../c3ref/close.html">sqlite3_close</a>
call that concludes the blocking connections transaction.</p>

<p>If sqlite3_unlock_notify() is called in a multi-threaded application,
there is a chance that the blocking connection will have already
concluded its transaction by the time sqlite3_unlock_notify() is invoked.
If this happens, then the specified callback is invoked immediately,
from within the call to sqlite3_unlock_notify().</p>

<p>If the blocked connection is attempting to obtain a write-lock on a
shared-cache table, and more than one other connection currently holds
a read-lock on the same table, then SQLite arbitrarily selects one of
the other connections to use as the blocking connection.</p>

<p>There may be at most one unlock-notify callback registered by a
blocked connection. If sqlite3_unlock_notify() is called when the
blocked connection already has a registered unlock-notify callback,
then the new callback replaces the old. If sqlite3_unlock_notify() is
called with a NULL pointer as its second argument, then any existing
unlock-notify callback is canceled. The blocked connections
unlock-notify callback may also be canceled by closing the blocked
connection using <a href="../c3ref/close.html">sqlite3_close()</a>.</p>

<p>The unlock-notify callback is not reentrant. If an application invokes
any sqlite3_xxx API functions from within an unlock-notify callback, a
crash or deadlock may be the result.</p>

<p>Unless deadlock is detected (see below), sqlite3_unlock_notify() always
returns SQLITE_OK.</p>

<p><b>Callback Invocation Details</b></p>

<p>When an unlock-notify callback is registered, the application provides a
single void* pointer that is passed to the callback when it is invoked.
However, the signature of the callback function allows SQLite to pass
it an array of void* context pointers. The first argument passed to
an unlock-notify callback is a pointer to an array of void* pointers,
and the second is the number of entries in the array.</p>

<p>When a blocking connections transaction is concluded, there may be
more than one blocked connection that has registered for an unlock-notify
callback. If two or more such blocked connections have specified the
same callback function, then instead of invoking the callback function
multiple times, it is invoked once with the set of void* context pointers
specified by the blocked connections bundled together into an array.
This gives the application an opportunity to prioritize any actions
related to the set of unblocked database connections.</p>

<p><b>Deadlock Detection</b></p>

<p>Assuming that after registering for an unlock-notify callback a
database waits for the callback to be issued before taking any further
action (a reasonable assumption), then using this API may cause the
application to deadlock. For example, if connection X is waiting for
connection Y's transaction to be concluded, and similarly connection
Y is waiting on connection X's transaction, then neither connection
will proceed and the system may remain deadlocked indefinitely.</p>

<p>To avoid this scenario, the sqlite3_unlock_notify() performs deadlock
detection. If a given call to sqlite3_unlock_notify() would put the
system in a deadlocked state, then SQLITE_LOCKED is returned and no
unlock-notify callback is registered. The system is said to be in
a deadlocked state if connection A has registered for an unlock-notify
callback on the conclusion of connection B's transaction, and connection
B has itself registered for an unlock-notify callback when connection
A's transaction is concluded. Indirect deadlock is also detected, so
the system is also considered to be deadlocked if connection B has
registered for an unlock-notify callback on the conclusion of connection
C's transaction, where connection C is waiting on connection A. Any
number of levels of indirection are allowed.</p>

<p><b>The "DROP TABLE" Exception</b></p>

<p>When a call to <a href="../c3ref/step.html">sqlite3_step()</a> returns SQLITE_LOCKED, it is almost
always appropriate to call sqlite3_unlock_notify(). There is however,
one exception. When executing a "DROP TABLE" or "DROP INDEX" statement,
SQLite checks if there are any currently executing SELECT statements
that belong to the same connection. If there are, SQLITE_LOCKED is
returned. In this case there is no "blocking connection", so invoking
sqlite3_unlock_notify() results in the unlock-notify callback being
invoked immediately. If the application then re-attempts the "DROP TABLE"
or "DROP INDEX" query, an infinite loop might be the result.</p>

<p>One way around this problem is to check the extended error code returned
by an sqlite3_step() call. If there is a blocking connection, then the
extended error code is set to SQLITE_LOCKED_SHAREDCACHE. Otherwise, in
the special "DROP TABLE/INDEX" case, the extended error code is just
SQLITE_LOCKED.
</p><p>See also lists of
  <a href="objlist.html">Objects</a>,
  <a href="constlist.html">Constants</a>, and
  <a href="funclist.html">Functions</a>.</p>
